#include "GameEngine.h"

void GameEngine::initialize(HWND hWnd)
{
	state = MENU;
	running = true;
	gamestart = false;

	// instance of engine
	pEngine = DX9Engine::getInstance();

	// get instance input
	pInput = Input::getInstance();

	// set initial text display color
	pEngine->setTextColor(D3DCOLOR_ARGB(255, 0, 0, 0));

	a.initialize(L"images/cursor.png");
	l.Init();

	// User Interface initialize
	// initializes all textures used in UI
	ui.initialize();
	igmenOn = false;

	//////							<DEBUG>							//////
#ifdef MYAPP_DEBUG_TRACE
	// set all mbdown and kbdown to false
	for(int i=0; i<MAX_MOUSEBUT; ++i) {
		mbdown[i] = false;
	}
	for(int i=0; i<MAX_KEYS; ++i) {
		kbdown[i] = false;
	}

	// initialize on screen display
	osd.initialize();
#endif
}

// Update GameEngine
void GameEngine::update(float dT)
{
	a.updatePosition(pInput->getMousePos());
	updateState();
	switch(state)
	{
	case MENU:
		ui.update();
		break;
	case GAME:
		if(pInput->keyPressed(DIK_RETURN))
			gamestart = true;
		if(gamestart && !igmenOn)
			l.update(dT);
		if(ui.isResume())
			igmenOn = false;
		// open/close IGMenu
		// if IGMenu is open
		// 'Pause' the game
		if(pInput->keyPressed(DIK_ESCAPE)) {
			if(igmenOn) {
				igmenOn = false;
				DbgPrintf("IGMenu not displaying\n");
			} else {
				igmenOn = true;
				DbgPrintf("IGMenu displaying\n");
			}
		}
		if(igmenOn) {
			ui.update();
		}
		if(l.gameOver()) {
			state = LOSE;
			break;
		}
		break;
	case WIN:
		break;
	case LOSE:
		//TODO: Add gameover screen
		if(pInput->keyPressed(DIK_RETURN)) {
			state = MENU;
		}
		break;
	}
#ifdef MYAPP_DEBUG_TRACE
	// checks for input and displays on console
	for(int i=0; i<MAX_MOUSEBUT; ++i) {
		if(pInput->mouseButtonDown(i) && !mbdown[i]) {
			mbdown[i] = true;
			DbgPrintf("Input > MouseButton[%d] > Down\n", i);
		} else {
			if(!pInput->mouseButtonDown(i) && mbdown[i]) {
				mbdown[i] = false;
				DbgPrintf("Input > MouseButton[%d] > Released\n", i);	
			}
		}
	}
	for(int i=0; i<MAX_KEYS; ++i) {
		if(pInput->keyDown(i) && !kbdown[i]) {
			kbdown[i] = true;
			DbgPrintf("Input > Keyboard[%d] > Down\n", i);
		} else {
			if(!pInput->keyDown(i) && kbdown[i]) {
				kbdown[i] = false;
				DbgPrintf("Input > Keyboard[%d] > Released\n", i);
			}
		}
	}

	osd.update(dT);
#endif
}

// Render GameEngine
void GameEngine::render()
{
	switch(state)
	{
	case MENU:
		ui.render();
		a.draw();
		break;
	case GAME:
		l.render();
		if(igmenOn)
		{
			ui.render();
		}
		a.draw();
		break;
	case WIN:
		break;
	case LOSE:
		break;
	}
}

void GameEngine::renderText()
{
	switch(state)
	{
	case MENU:
		ui.drawText();
		break;
	case GAME:
		if(!gamestart)
		{
			char buffer[100];
			RECT rect;
			rect.top = 0;
			rect.left = 0;
			rect.bottom = SCREEN_HEIGHT;
			rect.right = SCREEN_WIDTH;
			sprintf(buffer, "Press ENTER.");
			pEngine->writeTextCenter(buffer, rect);
		}
		if(igmenOn)
			ui.drawText();
		break;
	case WIN:
		break;
	case LOSE:
		break;
	}
#ifdef MYAPP_DEBUG_TRACE
	// draws on screen display
	osd.renderText();
#endif
}

void GameEngine::release()
{
	l.Release();
}